<?php

	require_once 'googleOptions.php';
	require_once 'googleSearch.php';
	require_once 'searchPager.php';
	require_once 'resources/Smarty/Smarty.class.php';

	class searchParser {

		private	$results		=	NULL;
		private	$smarty		=	NULL;
		private	$obj			=	NULL;



		public function __construct($jsonObj=NULL,$tplDir=NULL){

			if (!is_dir($tplDir)||is_null($tplDir)) throw (new Exception ("The given templates directory ->$tplDir<- doesnt exists!"));
	
			$this->obj		=	$jsonObj;
			$this->jsonObj	=	$jsonObj->responseData->results;
			$this->smartyInit($tplDir);
			$this->parse();

		}



		private function smartyInit ($tplDir=NULL) { 

			$tmpPath				= 'tmp';
			$smartyDir			= 'resources/Smarty/plugins';
			$this->smarty		=	new Smarty();

			$this->smarty->force_compile						= true;
                        $this->smarty->caching			= false;
                        $this->smarty->compile_check	= true;
                        $this->smarty->cache_lifetime	= -1;
                        $this->smarty->template_dir	= $tplDir;
                        $this->smarty->compile_dir		= $tmpPath;

			$this->smarty->plugins_dir    = array($smartyDir,'resources/plugins');


		}

		public function getSmartyObject(){

			return $this->smarty;

		}

		private function parse(){

			$c	=	1;

			foreach ($this->jsonObj as $result) {

				$this->smarty->assign('url_'      .$c  ,$result->url);
				$this->smarty->assign('content_'  .$c  ,$result->content);
				$this->smarty->assign('title_'    .$c++,$result->titleNoFormatting);

			}


		}


	}


?>
